package com.wh.web.util.lang;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

public class StringUtil {
	public static String[] toArray(String parseString, String splitString, boolean returnDelims) {
		StringTokenizer tokens = new StringTokenizer(parseString, splitString, returnDelims);
		String[] values = new String[tokens.countTokens()];
		int i = 0;
		while (tokens.hasMoreTokens()) {
			values[i++] = tokens.nextToken();
		}
		return values;
	}

	/**
	 * 格式化一个日期对象，默认的格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 *            Date
	 * @param pattern
	 *            String
	 * @return String
	 */
	public static String format(Date date, String pattern) {
		String fmtStr = "";
		if (date != null) {
			DateFormat df = new SimpleDateFormat(pattern);
			fmtStr = df.format(date);
		}
		return fmtStr;
	}

	/**
	 * 将一个字符串格式化为一个Date对象
	 * 
	 * @param obj
	 *            Object
	 * @return Date
	 */
	public static Date parse(Object obj) {
		try {
			if (obj == null) {
				return null;
			}
			String dateString = obj.toString().trim();
			if (dateString.length() == 0) {
				return null;
			}
			if (dateString.length() == 10) {
				dateString += " 00:00:00";
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.parse(dateString);
		} catch (ParseException ex) {
			ex.printStackTrace();
			return null;
		}

	}

	/**
	 * 判断一个字符串是否为空或者是右空白符号组成
	 * 
	 * @param source
	 *            String
	 * @return boolean
	 */
	public static boolean empty(String source) {
		return (source == null || source.trim().length() == 0 || source.toLowerCase().trim().equals("null")) ? true : false;
	}

	/**
	 * 判断一个字符串是否不为空或者不是右空白符号组成
	 * 
	 * @param source
	 *            String
	 * @return boolean
	 */
	public static boolean notEmpty(String source) {
		return (source != null && source.trim().length() > 0 && !source.toLowerCase().equals("null")) ? true : false;
	}

	public static String format(Date date) {
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}

}
